﻿<?php
if (!defined('BASE_PATH'))
    exit('No direct script access allowed');

/**
 * 忘道Php框架 
 * 
 * 作者 爱是小微笑 
 * 联系 QQ：549193902 EMAIL：549193902@qq.com
 * 2013-10-10 13:00:32
 */
// ------------------------------------------------------------------------
class loader {

    private static $loaded = array();
    //变量缓存
    protected $cache_vars		= array();

    /**
     * 初始自动加载类
     * @return \loader
     */
    public function initialize() {
        $this->input = $this->library("input", 'core');
        return $this;
    }

    public function database($conn = 'default') {
        require_once(SYSTEM_PATH . 'database/DB.php');
        $params = array(
            'dbdriver' => 'mysql',
            'hostname' => 'localhost',
            'username' => 'root',
            'password' => '',
            'database' => 'email',
            'pconnect' => TRUE,
            'db_debug' => FALSE,
            'cache_on' => FALSE,
            'char_set' => 'utf-8',
            'dbcollat' => 'utf8_general_ci',
        );
        $db = DB($params, TRUE);
        return $db;
    }

    /**
     * 模版布局
     * @param type $view 模版文件名称
     * @param type $param 传递变量
     * @param type $cache 是否直接输出
     * @return boolean
     */
    public function layout($view, $param = array(), $cache = FALSE) {
        $param['base_class'] = $this->input->url(0);
        $param['base_method'] = $this->input->url(1);
        $this->view(array('theme/head', $view, 'theme/foot'), $param, $cache);
    }

    /**
     * 加载数据库类
     * 
     * @param type $class
     * @param type $apppath
     * @return boolean
     */
    public function model($class) {
        if (is_file(APP_PATH . '/Model/' . $class . ".php")) {
            require APP_PATH . '/Model/' . $class . ".php";
        } else {
            echo 'model ' . $class . ' desn\'t exsit';
            return false;
        }
        $WD = & get_instance();
        if (isset($WD->$class)) {
            show_error('错误!该类名称已使用' . $class);
        }
        $WD->$class = new $class();
        $WD->$class->db = $this->database();
        return $WD->$class;
    }

    /**
     * 模版加载
     * @param type $view 模版文件名称
     * @param type $param 传递变量
     * @param type $cache 是否直接输出
     * @return boolean
     */
    public function view($view, $param = array(), $cache = FALSE) {

//        if (!empty($param)) {
//            extract($param);
//        }

        //模版文件加载控制器类
        $_ci_CI = & get_instance();
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) {
            if (!isset($this->$_ci_key)) {
                $this->$_ci_key = & $_ci_CI->$_ci_key;
            }
        }

        //写入变量
        if (is_array($param)) {
            $this->cache_vars = array_merge($this->cache_vars, $param);
        }
        extract($this->cache_vars);

        ob_start();

        if (is_array($view)) {
            foreach ($view as $v) {
                if (is_file(APP_PATH . '/View/' . $v . ".php")) {
                    require APP_PATH . '/View/' . $v . ".php";
                } else {
                    echo '模版 ' . $v . ' 不存在, 请建立';
                    return false;
                }
            }
        } else {
            if (is_file(APP_PATH . '/View/' . $view . ".php")) {
                require APP_PATH . '/View/' . $view . ".php";
            } else {
                echo '模版 ' . $view . ' 不存在, 请建立';
                return false;
            }
        }

        if ($cache === TRUE) {
            $buffer = ob_get_contents();
            @ob_end_clean();
            return $buffer;
        }
    }

    /**
     * 类加载器
     * 
     * @param type $class 类名称
     * @param type $directory 类型
     * @return class
     */
    public function library($class, $directory = 'core', $parameter = false) {
        require_once SYSTEM_PATH . $directory . '/' . $class . '.php';
        $WD = & get_instance();
        if (isset($WD->$class)) {
            show_error('错误!类库重复加载' . $class);
        }
        $WD->$class = new $class();
        return $WD->$class;
    }

}
?>